package com.atguigu.spzx.service.impl;

import com.atguigu.spzx.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.service.SysMenuService;
import com.atguigu.spzx.service.SysRoleMenuService;
import com.atguigu.spzx.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class SysRoleMenuServiceImpl implements SysRoleMenuService {


    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long id) {
        List<SysMenu> nodes = sysMenuService.findNodes();
        List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("sysMenuList", nodes);
        map.put("roleMenuIds", roleMenuIds);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //先删除后保存
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
        List<Map<String, Number>> menuIdList = assginMenuDto.getMenuIdList();
        if (!CollectionUtils.isEmpty(menuIdList)) {
            sysRoleMenuMapper.doAssign(assginMenuDto);
        }
    }
}
